// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using System.Xml;

namespace QuoteAdvantageEditor
{
	/// <summary>
	/// Dialog for specifying categories that are tied to the child product customization screen
	/// </summary>
   public class ChildCategoryDlg : System.Windows.Forms.Form
   {
      protected XmlNode objCategoryNode = null;

      private System.Windows.Forms.Label label1;
      private System.Windows.Forms.Button btnOK;
      private System.Windows.Forms.Button btnCancel;
      private System.Windows.Forms.Label label2;
      private System.Windows.Forms.TextBox txtCategoryName;
      private System.Windows.Forms.TextBox txtCategoryId;
      private System.Windows.Forms.Label label3;
      private System.Windows.Forms.ComboBox ddlCategoryType;
      /// <summary>
      /// Required designer variable.
      /// </summary>
      private System.ComponentModel.Container components = null;
      
      public XmlNode CategoryNode
      {
         get { return objCategoryNode; }
         set { objCategoryNode = value; }
      }

      public ChildCategoryDlg()
      {
         //
         // Required for Windows Form Designer support
         //
         InitializeComponent();

         //
         // TODO: Add any constructor code after InitializeComponent call
         //
      }

      /// <summary>
      /// Clean up any resources being used.
      /// </summary>
      protected override void Dispose( bool disposing )
      {
         if( disposing )
         {
            if(components != null)
            {
               components.Dispose();
            }
         }
         base.Dispose( disposing );
      }

      #region Windows Form Designer generated code
      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      private void InitializeComponent()
      {
         this.label1 = new System.Windows.Forms.Label();
         this.txtCategoryName = new System.Windows.Forms.TextBox();
         this.btnOK = new System.Windows.Forms.Button();
         this.btnCancel = new System.Windows.Forms.Button();
         this.txtCategoryId = new System.Windows.Forms.TextBox();
         this.label2 = new System.Windows.Forms.Label();
         this.label3 = new System.Windows.Forms.Label();
         this.ddlCategoryType = new System.Windows.Forms.ComboBox();
         this.SuspendLayout();
         // 
         // label1
         // 
         this.label1.Location = new System.Drawing.Point(16, 20);
         this.label1.Name = "label1";
         this.label1.Size = new System.Drawing.Size(88, 16);
         this.label1.TabIndex = 0;
         this.label1.Text = "Category Name:";
         this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
         // 
         // txtCategoryName
         // 
         this.txtCategoryName.Location = new System.Drawing.Point(108, 16);
         this.txtCategoryName.Name = "txtCategoryName";
         this.txtCategoryName.Size = new System.Drawing.Size(220, 21);
         this.txtCategoryName.TabIndex = 1;
         this.txtCategoryName.Text = "Secondary Hard Drive";
         // 
         // btnOK
         // 
         this.btnOK.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
         this.btnOK.Location = new System.Drawing.Point(172, 120);
         this.btnOK.Name = "btnOK";
         this.btnOK.TabIndex = 8;
         this.btnOK.Text = "OK";
         this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
         // 
         // btnCancel
         // 
         this.btnCancel.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
         this.btnCancel.Location = new System.Drawing.Point(252, 120);
         this.btnCancel.Name = "btnCancel";
         this.btnCancel.TabIndex = 9;
         this.btnCancel.Text = "Cancel";
         this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
         // 
         // txtCategoryId
         // 
         this.txtCategoryId.Location = new System.Drawing.Point(108, 48);
         this.txtCategoryId.Name = "txtCategoryId";
         this.txtCategoryId.Size = new System.Drawing.Size(220, 21);
         this.txtCategoryId.TabIndex = 3;
         this.txtCategoryId.Text = "MY_CATEGORY";
         // 
         // label2
         // 
         this.label2.Location = new System.Drawing.Point(16, 52);
         this.label2.Name = "label2";
         this.label2.Size = new System.Drawing.Size(88, 16);
         this.label2.TabIndex = 2;
         this.label2.Text = "Category ID:";
         this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
         // 
         // label3
         // 
         this.label3.Location = new System.Drawing.Point(16, 80);
         this.label3.Name = "label3";
         this.label3.Size = new System.Drawing.Size(88, 16);
         this.label3.TabIndex = 4;
         this.label3.Text = "Category Type:";
         this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
         // 
         // ddlCategoryType
         // 
         this.ddlCategoryType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
         this.ddlCategoryType.Location = new System.Drawing.Point(108, 76);
         this.ddlCategoryType.Name = "ddlCategoryType";
         this.ddlCategoryType.Size = new System.Drawing.Size(108, 21);
         this.ddlCategoryType.TabIndex = 5;
         // 
         // ChildCategoryDlg
         // 
         this.AcceptButton = this.btnOK;
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
         this.ClientSize = new System.Drawing.Size(338, 156);
         this.Controls.Add(this.ddlCategoryType);
         this.Controls.Add(this.label3);
         this.Controls.Add(this.label2);
         this.Controls.Add(this.txtCategoryId);
         this.Controls.Add(this.txtCategoryName);
         this.Controls.Add(this.label1);
         this.Controls.Add(this.btnCancel);
         this.Controls.Add(this.btnOK);
         this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
         this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
         this.MaximizeBox = false;
         this.MinimizeBox = false;
         this.Name = "ChildCategoryDlg";
         this.ShowInTaskbar = false;
         this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
         this.Text = "Category";
         this.Load += new System.EventHandler(this.ChildCategoryDlg_Load);
         this.ResumeLayout(false);

      }
      #endregion
      
      private void ChildCategoryDlg_Load(object sender, System.EventArgs e)
      {
         txtCategoryName.Text = objCategoryNode.Attributes["name"].Value;
         txtCategoryId.Text = objCategoryNode.Attributes["categoryId"].Value;
         RefreshTypeList();           
      }

      private void btnOK_Click(object sender, System.EventArgs e)
      {
         if (txtCategoryName.Text == "")
         {
            MessageBox.Show(this, "Please enter a name for this category", "QuoteAdvantage Editor");
            txtCategoryName.Focus();
         }
         else if (txtCategoryId.Text == "")
         {
            MessageBox.Show(this, "Please enter an ID for this category", "QuoteAdvantage Editor");
            txtCategoryId.Focus();
         }
         else
         {
            int nCategoryCount = CountCategories(txtCategoryId.Text, objCategoryNode.OwnerDocument);

            if (nCategoryCount > 0)
            {
               MessageBox.Show(this, "A category with this ID already exists. Please specify a different Category ID", "QuoteAdvantage Editor");
               txtCategoryId.Focus();
            }
            else
            {
               SaveData();
               this.DialogResult = DialogResult.OK;
               this.Close();
            }
         }
      }

      private void SaveData()
      {
         objCategoryNode.Attributes["name"].Value = txtCategoryName.Text;
         objCategoryNode.Attributes["categoryId"].Value = txtCategoryId.Text;    
         objCategoryNode.Attributes["type"].Value = ddlCategoryType.SelectedValue.ToString();  
      }

      private void btnCancel_Click(object sender, System.EventArgs e)
      {
         this.Close();
      }

      
      // Check for existing categories with this Category ID
      private int CountCategories(string strCategoryId, XmlDocument objDocument)
      {
         XmlNode objParentNode = objDocument.SelectSingleNode("//ConfigurationData");
         string strTarget = strCategoryId.ToUpper();
         int nTotal = 0;

         foreach (XmlNode node in objParentNode.ChildNodes)
         {
            string strCat = node.Attributes["categoryId"].Value.ToUpper();

            if (strCat == strTarget)
            {
               // Don't count the node that we're currently editing
               if (node != objCategoryNode)
               {
                  nTotal++;
               }
            }
         }

         return nTotal;
      }       

      // Initialize Category Type selection
      private void RefreshTypeList()
      {
         string strType = "SingleSelect";

         if (objCategoryNode.Attributes["type"] == null)
         {
            XmlAttribute objAttrib = objCategoryNode.OwnerDocument.CreateAttribute("", "type", "");
            objAttrib.Value = "SingleSelect";
            objCategoryNode.Attributes.Append(objAttrib);
         }
         else
         {
            strType = objCategoryNode.Attributes["type"].Value;   
         }
         
         ddlCategoryType.Items.Clear();
         ListItem item = new ListItem();
         ArrayList objItemList = new ArrayList(2);
     
         objItemList.Add(new ListItem("Single Select", "SingleSelect"));
         objItemList.Add(new ListItem("Multi Select", "MultiSelect"));
         
         ddlCategoryType.DisplayMember = "Text";
         ddlCategoryType.ValueMember = "Value";
         ddlCategoryType.DataSource = objItemList;

         ddlCategoryType.SelectedValue = strType;
      }



   } // end class
}
